package com.zhiyou100.taxi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.taxi.dao.CarInMapper;
import com.zhiyou100.taxi.entity.CarExample;
import com.zhiyou100.taxi.entity.CarIn;
import com.zhiyou100.taxi.entity.CarInExample;
import com.zhiyou100.taxi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-25 17:28
 * @Description
 */
@Service
@Transactional
public class CarInServiceImpl implements CarInService {
    @Autowired
    CarInMapper carInMapper;

    @Override
    public int deleteByCarId(String crId) {
        CarInExample example = new CarInExample();
        CarInExample.Criteria criteria = example.createCriteria();
        criteria.andCarIdEqualTo(crId);
        return carInMapper.deleteByExample(example);
    }

    @Override
    public int deleteByCarOutId(String carOutId) {
        CarInExample example = new CarInExample();
        CarInExample.Criteria criteria = example.createCriteria();
        criteria.andCarOutIdEqualTo(carOutId);
        return carInMapper.deleteByExample(example);
    }


    @Override
    public PageInfo<CarIn> pageCar(Integer pageNum) {
        PageHelper.startPage(pageNum, 5);
        PageInfo<CarIn> carInPageInfo = new PageInfo<>(getAll());
        return carInPageInfo;
    }

    @Override
    public List<CarIn> getAll() {
        return carInMapper.selectByExample(null);
    }

//    @Autowired
//    UserService userService;
//    @Autowired
//    CarService carService;
//    @Autowired
//    CarCheckService carCheckService;
//    @Autowired
//    CarOutService carOutService;

    //批量删除包括外键)想多了吗
    @Override
    public int deleteByPrimaries(HashSet<String> set) {
        try {
            for (String s : set) {
                //CarIn carIn = getOneByPrimaryKey(s);
                carInMapper.deleteByPrimaryKey(s);
            }
            return set.size();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public CarIn getOneByPrimaryKey(String carInId) {
        return carInMapper.selectByPrimaryKey(carInId);
    }

    @Override
    public CarIn getOneUnionByPrimaryKey(String carInId) {

        return carInMapper.selectByPrimaryKeyUnion(carInId);
    }

    @Override
    public String getPrimaryKey() {
        return "car_id_" + carInMapper.selectPrimaryKey();
    }

    @Override
    public int insert(CarIn carIn) {
        return carInMapper.insert(carIn);
    }

    @Override
    public int updateByPrimaryKey(CarIn carIn) {
        return carInMapper.updateByPrimaryKey(carIn);
    }
}
